<?php
declare (strict_types = 1);

namespace app\model;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Blogs extends Model
{
    protected $pk ='bid';
    //移动端 获取抽屉
    public static function getDrawer(){
        return Db::name('blogs')->select();
}
    //文章访问量更新
    public static function updateVisited($bid){
        $visited = Blogs::where('bid',$bid)->value('visited');
        return Blogs::update(['visited'=>$visited+1,'bid'=>$bid]);
    }
    //上一篇，下一篇
    public static function getBlogNext($bid,$symbol,$order){
        return Blogs::where('bid',$symbol,$bid)->order('bid',$order)->limit(1)->select();
    }
    //根据年月获取文章列表
    public static function getYearDayBlogList($date,$limit=10,$page=1){
//        $sql = "SELECT * FROM blogs WHERE FROM_UNIXTIME(uploaddate,'%Y-%m') = "."'".$date."'";
        $sql = "select blogs.*,blogtype.typename FROM blogs ";
        $sql.="LEFT JOIN blogtype on blogs.tid=blogtype.tid ";
        $sql.="WHERE FROM_UNIXTIME(blogs.uploaddate,'%Y-%m') = "."'".$date."' ORDER BY blogs.uploaddate desc ";
        $sql.="LIMIT ". ($page - 1) * $limit . ',' . $limit;
        $sqlCount = "SELECT COUNT(*) FROM blogs WHERE FROM_UNIXTIME(uploaddate,'%Y-%m') = "."'".$date."'";
        $data = Db::query($sql);
        $countData = Db::query($sqlCount);
        return ['data'=>$data,'count'=>$countData];
    }
    //根据tid获取文章列表
    public static function getTidBlogList($tid,$limit=10,$page=1){
        $data =Db::table('blogs')
            ->alias('a')
            ->leftJoin('blogtype b','a.tid=b.tid')
            ->where('a.tid',$tid)
            ->order('uploaddate','desc')
            ->paginate(['list_rows'=>$limit,'page'=>$page]);
        return $data;
    }
    //获取搜索列表
    public static function getSearch($key = '',$limit=10,$page=1){
        $where=['title','like',"%".$key."%"];
        $data = Db::table('blogs')
            ->alias('a')
            ->leftJoin('blogtype b','a.tid=b.tid')
            ->where([$where])
            ->order('uploaddate','desc')
            ->paginate(['list_rows'=>$limit,'page'=>$page]);
        return $data;
}
    //按年月归档
    public static function getYearMonth(){
        $sql=" SELECT FROM_UNIXTIME(uploaddate,'%Y-%m') date,count(bid) count FROM blogs GROUP BY date";
        $data=Db::query($sql);
        return $data;
    }
    //按分类归档
    public static function getCategoryList(){
        $sql = " SELECT blogs.tid,blogtype.typename,count(blogs.tid) count FROM blogs LEFT JOIN blogtype on blogs.tid=blogtype.tid GROUP BY tid";
        $data=Db::query($sql);
        return $data;
    }
    //热文 top10
    public static function hotBlog(){
        return Db::name('Blogs')->limit(10)->order('uploaddate desc')->select();
    }
    //获取无限级分类
    public function cate($arrs,$level=0,$sid=0)	{
        static $categorys = array();
        //循环原始的分类数组
        foreach($arrs as $arr)
        {
            //查找下级菜单
            if($arr['sid']==$sid)
            {
                $arr['level'] = $level;
                $arr['html']=str_repeat('---',$level*4);
                $categorys[] = $arr;
                //递归调用
                $this->cate($arrs,$level+1,$arr['tid']);
            }
        }
        //返回结果
        return $categorys;
    }
    //递归获取所有的子菜单的ID
    function get_all_child($array,$tid){
        $arr = array();
        foreach($array as $v){
            if($v['sid'] === $tid){
                $arr[] = $v['tid'];
                $this->get_all_child($array,$v['tid']);
            };
        };
        return $arr;
    }
    //删除无限级分类
    public function delCate($getTid){
        $array=Db::name('blogtype')->select();
        $delTid=$this->get_all_child($array,(int)$getTid);
        $delTid[]=array_push($delTid,$getTid);
        $res=Blogtype::destroy($delTid);
        if ($res){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }
    }
    //获取博客列表数据 table展示
    public function getBlogList($page=1,$limit=10,$keyword=''){
        $count = $this->db('blogs')->count();//总数
        if (isset($keyword)){
            $sql="SELECT  blogs.*,blogtype.typename,waytype.waytype FROM blogs ";
            $sql.="LEFT JOIN waytype on blogs.wid=waytype.wid ";
            $sql.="LEFT JOIN blogtype on blogs.tid=blogtype.tid ";
            $sql.="where title like '"."%".$keyword."%"."' ORDER BY bid desc ";
            $sql.="LIMIT ". ($page - 1) * $limit . ',' . $limit;
            $count=Db::name('blogs')
                ->where('title','like',"%".$keyword."%")
                ->count();
        }else{
            $sql="SELECT  blogs.*,blogtype.typename FROM blogs ";
            $sql.="LEFT JOIN waytype on blogs.wid=waytype.wid ";
            $sql.="LEFT JOIN blogtype on blogs.tid=blogtype.tid ";
            $sql.="ORDER BY bid desc ";
            $sql.="LIMIT ". ($page - 1) * $limit . ',' . $limit;
        }
        $data = Db::query($sql);
        foreach ($data as &$v){
            $v['url']=DOMAIN_NAME.$v['pic'];
        }
        return ['data'=>$data,'count'=>$count];
    }
}
